package cz.khfotbal.beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.khfotbal.domain.Competion;
import cz.khfotbal.domain.Contact;
import cz.khfotbal.domain.Delegationczech;
import cz.khfotbal.domain.Match;
import cz.khfotbal.service.ContactService;
import cz.khfotbal.service.DelegationczechService;
import cz.khfotbal.service.MatchService;

@Component
@Scope(value = "session")
public class DelegationBean {

	@Autowired
	private MatchService matchService;

	@Autowired
	private ContactService contactService;
	@Autowired
	private DelegationczechService delegationczechService;

	private Match selectedMatch;
	private String delegationDate;
	private String delegtionDateTo;
	private List<Match> matchList = new ArrayList<Match>();
	private List<Delegationczech> delegationczech = new ArrayList<Delegationczech>();
	
	private List<Competion> competionList = new ArrayList<Competion>();
	private boolean changes;

	private String date;
	private String time;
	private String delegation;
	private String ride;
	private String refree;
	private String arOne;
	private String arTwo;

	private List<String> delegatNamesList = new ArrayList<String>();
	private List<String> refereeNamesList = new ArrayList<String>();
	

	public void fillMatchListDate() {
		changes = false;
		Date dateFrom = dateString(delegationDate);
		Date dateTo = dateString(delegtionDateTo);
		long pom = ((dateTo.getTime() - dateFrom.getTime()) / 1000 / 60 / 60 / 24);
		matchList.clear();
		int days = (int) pom;
		days++;
		for (int i = 0; i < days; i++) {
			String date = stringDate(dateFrom);
			matchList.addAll(matchService.getMatchesByDate(date));
			System.out.println(date);
			dateFrom.setDate(dateFrom.getDate() + 1);
		}
	}

	private Date dateString(String sDate) {
		System.out.println(sDate);
		String[] split = sDate.split("\\.");
		int year = Integer.parseInt(split[2]);
		int month = Integer.parseInt(split[1]);
		int day = Integer.parseInt(split[0]);
		Date date = new Date(year - 1900, month - 1, day);
		return date;
	}

	public void fillMatchListThisWeek() {
		Date actualDate = new Date();
		int day = actualDate.getDay();
		if (actualDate.getDay()==0){
			day = 7;
		}
		int dataShift = 6 - day;
		actualDate.setDate(actualDate.getDate() + dataShift);
		matchList = matchService.getMatchesByDate(stringDate(actualDate));
		delegationczech = delegationczechService.getAllDelegationCzechDate(stringDate(actualDate));
		actualDate.setDate(actualDate.getDate() + 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 2);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		fillStyle();
	}

	public void fillMatchListThisWeekChanges() {
		Date actualDate = new Date();
		int day = actualDate.getDay();
		if (actualDate.getDay()==0){
			day = 7;
		}
		int dataShift = 6 - day;
		actualDate.setDate(actualDate.getDate() + dataShift);
		System.out.println(stringDate(actualDate));
		matchList = matchService.getMatchesByDate(stringDate(actualDate));
		actualDate.setDate(actualDate.getDate() + 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 2);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech = new ArrayList<Delegationczech>();
		fillStyle();
		List<Match> matchListPom = new ArrayList<Match>();
		for (Match match : matchList) {
			if (match.isZvyraznit()) {
				matchListPom.add(match);
			}
		}
		matchList = matchListPom;
	}

	public void fillMatchListNextWeek() {
		Date actualDate = new Date();
		int day = actualDate.getDay();
		if (actualDate.getDay()==0){
			day = 7;
		}
		int dataShift = 13 - day;
		actualDate.setDate(actualDate.getDate() + dataShift);
		matchList = matchService.getMatchesByDate(stringDate(actualDate));
		delegationczech = delegationczechService.getAllDelegationCzechDate(stringDate(actualDate));
		actualDate.setDate(actualDate.getDate() + 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 2);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		actualDate.setDate(actualDate.getDate() - 1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
		delegationczech.addAll(delegationczechService.getAllDelegationCzechDate(stringDate(actualDate)));
		fillStyle();
	}

	private void fillStyle() {
		competionList = new ArrayList<Competion>();
		for (Match match : matchList) {
			if (!isCompetionInList(match.getCompetion())) {
				competionList.add(match.getCompetion());
			}
			if (match.isDelegatChange()) {
				match.setDelegat("<div style='color:red'>" + match.getDelegat()
						+ "</div>");
			}
			if (match.isDatumChange()) {
				match.setDatum("<b style='color:red'>" + match.getDatum()
						+ "</b>");
			}
			if (match.isLineRefereeChange()) {
				match.setLineReferee("<div style='color:red'>"
						+ match.getLineReferee() + "</div>");
			}
			if (match.isLineRefereeTwoChange()) {
				match.setLineRefereeTwo("<div style='color:red'>"
						+ match.getLineRefereeTwo() + "</div>");
			}
			if (match.isMainRefereeChange()) {
				match.setMainReferee("<div style='color:red'>"
						+ match.getMainReferee() + "</div>");
			}
			if (match.isSpolujizdaChange()) {
				match.setSpolujizda("<div style='color:red'>"
						+ match.getSpolujizda() + "</div>");
			}
			if (match.isStartChange()) {
				match.setStart("<b style='color:red'>" + match.getStart()
						+ "</b>");
			}
		}
	}

	private boolean isCompetionInList(Competion competion) {
		for (Competion c : competionList) {
			if (c.getName().equals(competion.getName())) {
				return true;
			}
		}
		return false;
	}

	public void dataForCompare() {
		date = selectedMatch.getDatum();
		time = selectedMatch.getStart();
		delegation = selectedMatch.getDelegat();
		ride = selectedMatch.getSpolujizda();
		refree = selectedMatch.getMainReferee();
		arOne = selectedMatch.getLineReferee();
		arTwo = selectedMatch.getLineRefereeTwo();

		fillDelegatNames();
		fillRefereeNames();

	}

	private void fillDelegatNames() {
		List<Contact> cList = contactService.getAllContacts("delegat");
		delegatNamesList = new ArrayList<String>();
		for (Contact c : cList) {
//			delegatNamesList.add(c.getFirstname() + " " + c.getSurname());
			delegatNamesList.add(c.getSurname());
		}
	}

	private void fillRefereeNames() {
		List<Contact> cList = contactService.getAllContacts("rozhodci");
		refereeNamesList = new ArrayList<String>();
		for (Contact c : cList) {
//			refereeNamesList.add(c.getFirstname() + " " + c.getSurname());
			refereeNamesList.add(c.getSurname());
		}
	}

	public List<String> completeDelegats(String query) {
		List<String> result = new ArrayList<String>();
		for (String name : delegatNamesList) {
			if (name.toLowerCase().contains(query.toLowerCase())) {
				result.add(name);
			}
		}
		return result;
	}

	public List<String> completeReferees(String query) {
		List<String> result = new ArrayList<String>();
		for (String name : refereeNamesList) {
			if (name.toLowerCase().contains(query.toLowerCase())) {
				result.add(name);
			}
		}
		return result;
	}

	public void updateMatch() {
		if (changes) {
			if (!date.equals(selectedMatch.getDatum())) {
				selectedMatch.setDatumChange(true);
				selectedMatch.setZvyraznit(true);
			}
			if (!time.equals(selectedMatch.getStart())) {
				selectedMatch.setStartChange(true);
				selectedMatch.setZvyraznit(true);
			}
			if (!delegation.equals(selectedMatch.getDelegat())) {
				selectedMatch.setDelegatChange(true);
				selectedMatch.setZvyraznit(true);
			}
			if (!ride.equals(selectedMatch.getSpolujizda())) {
				selectedMatch.setSpolujizdaChange(true);
				selectedMatch.setZvyraznit(true);
			}
			if (!refree.equals(selectedMatch.getMainReferee())) {
				selectedMatch.setMainRefereeChange(true);
				selectedMatch.setZvyraznit(true);
			}
			if (!arOne.equals(selectedMatch.getLineReferee())) {
				selectedMatch.setLineRefereeChange(true);
				selectedMatch.setZvyraznit(true);
			}
			if (!arTwo.equals(selectedMatch.getLineRefereeTwo())) {
				selectedMatch.setLineRefereeTwoChange(true);
				selectedMatch.setZvyraznit(true);
			}
		}
		matchService.updateMatch(selectedMatch);
		selectedMatch = new Match();
		changes = false;
	}

	public void clearMatch() {
		selectedMatch.setDatumChange(false);
		selectedMatch.setStartChange(false);
		selectedMatch.setDelegatChange(false);
		selectedMatch.setSpolujizdaChange(false);
		selectedMatch.setMainRefereeChange(false);
		selectedMatch.setLineRefereeChange(false);
		selectedMatch.setLineRefereeTwoChange(false);
		selectedMatch.setZvyraznit(false);
		matchService.updateMatch(selectedMatch);
		selectedMatch = new Match();
	}

	private String stringDate(Date date) {
		String day;
		String month;
		int iDay = date.getDate();
		if (iDay < 10) {
			day = "0" + iDay;
		} else {
			day = Integer.toString(iDay);
		}

		int iMonth = date.getMonth() + 1;
		if (iMonth < 10) {
			month = "0" + iMonth;
		} else {
			month = Integer.toString(iMonth);
		}
		String year = Integer.toString(date.getYear() + 1900);
		String sPlayDay = day + "." + month + "." + year;
		return sPlayDay;
	}

	public List<Match> getMatchList() {
		return matchList;
	}

	public void setMatchList(List<Match> matchList) {
		this.matchList = matchList;
	}

	public String getDelegationDate() {
		return delegationDate;
	}

	public void setDelegationDate(String delegationDate) {
		this.delegationDate = delegationDate;
	}

	public Match getSelectedMatch() {
		if (selectedMatch == null) {
			selectedMatch = new Match();
		}
		return selectedMatch;
	}

	public void setSelectedMatch(Match selectedMatch) {
		this.selectedMatch = selectedMatch;
	}

	public boolean isChanges() {
		return changes;
	}

	public void setChanges(boolean changes) {
		this.changes = changes;
	}

	public List<String> getDelegatNamesList() {
		return delegatNamesList;
	}

	public void setDelegatNamesList(List<String> delegatNamesList) {
		this.delegatNamesList = delegatNamesList;
	}

	public List<String> getRefereeNamesList() {
		return refereeNamesList;
	}

	public void setRefereeNamesList(List<String> refereeNamesList) {
		this.refereeNamesList = refereeNamesList;
	}

	public String getDelegtionDateTo() {
		return delegtionDateTo;
	}

	public void setDelegtionDateTo(String delegtionDateTo) {
		this.delegtionDateTo = delegtionDateTo;
	}

	public List<Competion> getCompetionList() {
		return competionList;
	}
	
	public List<Competion> getCompetionListThis() {
		return competionList;
	}
	
	public List<Competion> getCompetionListNext() {
		return competionList;
	}
	
	public List<Competion> getCompetionListChanges() {
		return competionList;
	}

	public void setCompetionList(List<Competion> competionList) {
		this.competionList = competionList;
	}

	public List<Delegationczech> getDelegationczech() {
		return delegationczech;
	}

	public void setDelegationczech(List<Delegationczech> delegationczech) {
		this.delegationczech = delegationczech;
	}

}
